import { createRouter, createWebHashHistory } from 'vue-router'
import { isAuthenticated } from '../utils/auth.js'
import HomePage from '../HomePage.vue'

const LoginPage = () =>
    import ('../pages/LoginPage.vue')
const DashboardPage = () =>
    import ('../pages/DashboardPage.vue')
const LearnPage = () =>
    import ('../pages/LearnPage.vue')
const ReviewPage = () =>
    import ('../pages/ReviewPage.vue')
const LibraryPage = () =>
    import ('../pages/LibraryPage.vue')
const ProfilePage = () =>
    import ('../pages/ProfilePage.vue')
const AccountInfoPage = () =>
    import ('../pages/AccountInfoPage.vue')
const MoreSettingsPage = () =>
    import ('../pages/MoreSettingsPage.vue')
const CompletionPage = () =>
    import ('../pages/CompletionPage.vue')
const SummaryPage = () =>
    import ('../pages/SummaryPage.vue')
const SpellingPage = () =>
    import ('../pages/SpellingPage.vue')

const routes = [
    { path: '/', name: 'home', component: HomePage, meta: { requiresAuth: true } },
    { path: '/login', name: 'login', component: LoginPage },
    { path: '/dashboard', name: 'dashboard', component: DashboardPage, meta: { requiresAuth: true } },
    { path: '/learn', name: 'learn', component: LearnPage, meta: { requiresAuth: true } },
    { path: '/review', name: 'review', component: ReviewPage, meta: { requiresAuth: true } },
    { path: '/library', name: 'library', component: LibraryPage, meta: { requiresAuth: true } },
    { path: '/profile', name: 'profile', component: ProfilePage, meta: { requiresAuth: true } },
    { path: '/account-info', name: 'account-info', component: AccountInfoPage, meta: { requiresAuth: true } },
    { path: '/more-settings', name: 'more-settings', component: MoreSettingsPage, meta: { requiresAuth: true } },
    { path: '/completion', name: 'completion', component: CompletionPage, meta: { requiresAuth: true } },
    { path: '/summary', name: 'summary', component: SummaryPage, meta: { requiresAuth: true } },
    { path: '/spelling', name: 'spelling', component: SpellingPage, meta: { requiresAuth: true } },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 路由守卫：检查登录状态
router.beforeEach((to, from, next) => {
    // 如果路由需要认证
    if (to.meta.requiresAuth) {
        // 检查是否已登录
        if (isAuthenticated()) {
            // 已登录，允许访问
            next()
        } else {
            // 未登录，跳转到登录页
            next({
                path: '/login',
                query: { redirect: to.fullPath } // 保存原本要访问的路径，登录后可以跳转回去
            })
        }
    } else if (to.path === '/login') {
        // 如果访问登录页，且已经登录，则跳转到首页或redirect指向的页面
        if (isAuthenticated()) {
            // 如果有 redirect 参数，跳转到目标页面，否则跳转首页
            const redirect = to.query.redirect;
            if (redirect) {
                next(redirect)
            } else {
                next('/')
            }
        } else {
            // 未登录，允许访问登录页
            next()
        }
    } else {
        // 不需要认证的路由，直接访问
        next()
    }
})

export default router